﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Entity;
using Business.IManager;
using Business.Manager;
using Utility;
using System.Data;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using System.Collections;

namespace OnlinePayment.Admin
{
    public partial class PaymentCharView : System.Web.UI.Page
    {
        IAcademyManager academyManager = new AcademyManager();
        ISearchManager searchManager = new SearchManager();
        IApartmentManager apartmentManager = new ApartmentManager();
        IUtilityPaymentManager utilityPaymentManager = new UtilityPaymentManager();
        IChartManager chartManager = new ChartManager();

        /// <summary>
        /// 搜索结果开关，根据不同的搜索结果显示不同的数据
        /// 总共拥有选项：学费，学费详细，双专业费，重修费，水电费，校园网费，学生卡费
        /// </summary>
        //public String switchSearchedResult = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            //String switchSearchedResult = ViewState["SearchResultSwitch"].ToString();
            
            if (!IsPostBack)
            {
                
                setAllDivVisibleMode(false);
                init();
                ViewState["OrderDir"] = "ASC";
                ViewState["SearchResultSwitch"] = "";
                ViewState["ResultType"] = "Report";     //有Report和Chart切换结果显示界面
                ViewState["PaymentTypeChart"] = "";
                



                AT.Items.Add(new System.Web.UI.WebControls.ListItem("柱状图",FanG.Chartlet.AppearanceStyles.Bar_2D_Aurora_GlassCrystal_Glow_NoBorder.ToString()));
                AT.Items.Add(new System.Web.UI.WebControls.ListItem("折线图", FanG.Chartlet.AppearanceStyles.Line_2D_Aurora_ThinRound_NoGlow_NoBorder.ToString()));
                AT.Items.Add(new System.Web.UI.WebControls.ListItem("饼图图", FanG.Chartlet.AppearanceStyles.Pie_3D_Breeze_NoCrystal_NoGlow_NoBorder.ToString()));
                AT.Items.Add(new System.Web.UI.WebControls.ListItem("横向柱状图", FanG.Chartlet.AppearanceStyles.HBar_2D_Breeze_FlatCrystal_Glow_TextureBorder.ToString()));
                

                AT.Text = "柱状图";
            }
            else
            {
            }
            Chartlet1.Height = 600;
            Chartlet1.Width = 800;
            Chartlet1.InflateTop = 30;
            Chartlet1.InflateLeft = 30;
            Chartlet1.InflateRight = 30;
            Chartlet1.InflateBottom = 30;

            SetApp();
            //LoadData(null);
        }

        protected void AT_SelectedIndexChanged(object sender, EventArgs e)
        {
            SetApp();
            //LoadData(null);
            //Chartlet1.BindChartData((DataSet)ViewState["ChartData"]);
            //Chartlet1.YLabels.UnitText = "人数";
            string text = ddl_type.SelectedItem.Text.ToString();
            generateChart(text);
        }

        public void SetApp()
        {
            String AppearanceStyleName = AT.SelectedItem.Value.ToString();
            Chartlet1.AppearanceStyle = (FanG.Chartlet.AppearanceStyles)System.Enum.Parse(typeof(FanG.Chartlet.AppearanceStyles), AppearanceStyleName, true);
        }

        /**** 用来绑定图表的数据 ****/
        public void LoadData(String chartTitle,ArrayList coloerGuider,ArrayList xLabel,ArrayList[] dataArray)
        {

            ArrayList ColorGuider = new ArrayList();
            ArrayList[] DataArray = { new ArrayList(), new ArrayList(), new ArrayList() };
            ArrayList XLabel = new ArrayList();

            //1.Set data by manual, you can read data from database use below statements
            //手动添加数据，你也可以使用下面的代码从数据库中读取数据
            ColorGuider.Add("Laura");
            ColorGuider.Add("Jesy");
            ColorGuider.Add("Linda");

            XLabel.Add("East");
            XLabel.Add("South");
            XLabel.Add("West");
            XLabel.Add("North");

            //Chartlet1.TipsShow = false;
            DataArray[0].Add(98.9);
            DataArray[0].Add(45.2);
            DataArray[0].Add(33.69);
            DataArray[0].Add(60.20);


            DataArray[1].Add(76.1);
            DataArray[1].Add(68);
            DataArray[1].Add(43);
            DataArray[1].Add(10);

            DataArray[2].Add(10);
            DataArray[2].Add(77);
            DataArray[2].Add(15);
            DataArray[2].Add(79);

            Chartlet1.ChartTitle.Text = "Chartlet Demo";
            Chartlet1.XLabels.UnitText = "Region";
            Chartlet1.YLabels.UnitText = "Yield / %";
            Chartlet1.InitializeData(DataArray, XLabel, ColorGuider);

        }




























        private void init()
        {
            ddl_major.Items.Clear();
            ddl_grade.Items.Clear();
            ddl_year.Items.Clear();
            DataTable major = academyManager.getMajorTable();
            ddl_major.Items.Insert(0,new System.Web.UI.WebControls.ListItem("请选择专业", "0"));
            for (int i = 0; i < major.Rows.Count; i++)
            {
                DataRow row = major.Rows[i];
                ddl_major.Items.Add(new System.Web.UI.WebControls.ListItem(row["majName"].ToString(), row["majId"].ToString()));
            }

            int nowGrade = Convert.ToInt32(DateTime.Now.Year);
            ddl_grade.Items.Add(new System.Web.UI.WebControls.ListItem("请选择年级", "0"));
            ddl_year.Items.Add(new System.Web.UI.WebControls.ListItem("请选择缴费年份", "0"));
            //ddl_uitilityYear.Items.Add(new System.Web.UI.WebControls.ListItem("请选择缴费年份", "0"));
            for (int i = 2009; i <=nowGrade; i++)
            {
                ddl_grade.Items.Add(new System.Web.UI.WebControls.ListItem(Convert.ToString(i)+ "级", Convert.ToString(i)));
                ddl_year.Items.Add(new System.Web.UI.WebControls.ListItem(Convert.ToString(i) + "年", Convert.ToString(i)));
                ddl_uitilityYear.Items.Add(new System.Web.UI.WebControls.ListItem(Convert.ToString(i) + "年", Convert.ToString(i)));
                ddl_NB_Year.Items.Add(new System.Web.UI.WebControls.ListItem(Convert.ToString(i) + "年", Convert.ToString(i)));
            }

            //ddl_uitilityMonth.Items.Add(new System.Web.UI.WebControls.ListItem("请选择缴费月份", "0"));
            for (int i=1;i<=12;i++)
            {
                ddl_uitilityMonth.Items.Add(new System.Web.UI.WebControls.ListItem(Convert.ToString(i)+"月", Convert.ToString(i)));
                ddl_NB_Month.Items.Add(new System.Web.UI.WebControls.ListItem(Convert.ToString(i) + "月", Convert.ToString(i)));
            }

            ddl_academy.Items.Insert(0, new System.Web.UI.WebControls.ListItem("请选择公寓楼", "0"));
            DataTable apaTable = apartmentManager.getAllApartmentTable();
            for (int i = 0; i < apaTable.Rows.Count; i++)
            {
                DataRow row = apaTable.Rows[i];
                ddl_apartment.Items.Add(new System.Web.UI.WebControls.ListItem(row["apaName"].ToString(), row["apaId"].ToString()));
                ddl_NB_Apartment.Items.Add(new System.Web.UI.WebControls.ListItem(row["apaName"].ToString(), row["apaId"].ToString()));
            }


        }




        private void loadClassDDL()
        {
            ddl_class.Items.Clear();
            ddl_class.Items.Insert(0,new System.Web.UI.WebControls.ListItem("请选择班级", "0"));
            string acaValue = ddl_academy.SelectedItem.Value.ToString();
            string majValue = ddl_major.SelectedItem.Value.ToString();
            string graValue = ddl_grade.SelectedItem.Value.ToString();
            bool chosedAcademy = !acaValue.Equals("0");
            bool chosedMajor = !majValue.Equals("0");
            bool chosedGrade = !graValue.Equals("0");
            DataSet classDataSet = null;

            if (chosedAcademy && chosedMajor && chosedGrade){
                classDataSet = academyManager.getClassSetByMajorIdAndGrade(majValue, Convert.ToInt32(graValue));
                DataRowCollection rows = classDataSet.Tables[0].Rows;
                int classNum = rows.Count;
                for (int i = 0; i < classNum; i++)
                {
                    DataRow row = rows[i];
                    ddl_class.Items.Add(new System.Web.UI.WebControls.ListItem(row["className"].ToString(), row["classId"].ToString()));
                }
            } else if (!chosedAcademy && chosedMajor && chosedGrade){

            } else if (chosedAcademy && !chosedMajor && chosedGrade){

            } else if (chosedAcademy && chosedMajor && !chosedGrade) {

            } else if (!chosedAcademy && !chosedMajor && chosedGrade) {

            } else if (chosedAcademy && !chosedMajor && !chosedGrade) {

            } else if (!chosedAcademy && chosedMajor && !chosedGrade) {

            } else if (!chosedAcademy && !chosedMajor && !chosedGrade) {
                
            } else {

            } 


        }

        private void clearAllDDLSelected()
        {
            ddl_academy.Items.Clear();
            ddl_major.Items.Clear();
            //ddl_grade.Items.Clear();
            ddl_year.Items.Clear();
        }

        protected void ddl_type_SelectedIndexChanged(object sender, EventArgs e)
        {
            string text = ddl_type.SelectedItem.Text.ToString();
            GV_SelectedResult.DataSource = null;
            GV_SelectedResult.DataBind();
            switch (text)
            {
                case "请选择":
                    ViewState["SearchResultSwitch"] = "";
                    setAllDivVisibleMode(false);
                    clearAllDDLSelected();
                    break;
                case "学费":
                    ViewState["SearchResultSwitch"] = "学费";
                    ddl_academy.Items.Clear();
                    ddl_academy.Items.Insert(0, new System.Web.UI.WebControls.ListItem("请选择学院", "0"));
                    DataTable academys = academyManager.getAcademyTable();
                    for (int i = 0; i < academys.Rows.Count;i++ )
                    {
                        DataRow row = academys.Rows[i];
                        ddl_academy.Items.Add(new System.Web.UI.WebControls.ListItem(row["acaName"].ToString(), row["acaId"].ToString()));
                    }
                    setAllDivVisibleMode(false);
                    searchTuition.Visible = true;
                    break;
                case "双专业费":
                    ViewState["SearchResultSwitch"] = "双专业费";
                    setAllDivVisibleMode(false);
                    searchTuition.Visible = true;
                    break;
                case "水电费":
                    ViewState["SearchResultSwitch"] = "水电费";
                    

                    setAllDivVisibleMode(false);
                    searchUtility.Visible = true;
                    break;
                case "校园网费":
                    ViewState["SearchResultSwitch"] = "校园网费";


                    setAllDivVisibleMode(false);
                    searchNetworkBill.Visible = true;
                    break;
                case "学生卡充值":
                    ViewState["SearchResultSwitch"] = "学生卡充值";
                    setAllDivVisibleMode(false);
                    searchStudentCard.Visible = true;
                    break;
                default:
                    break;
            }
            loadClassDDL();
        }


        private void setAllDivVisibleMode(bool mode)
        {
            searchTuition.Visible = mode;
            searchUtility.Visible = mode;
            searchNetworkBill.Visible = mode;
            searchStudentCard.Visible = mode;
        }


        /*学院DDL改变*/
        protected void ddl_academy_SelectedIndexChanged(object sender, EventArgs e)
        {
            //ViewState["ResultType"] = "Report";
            String resultType = ViewState["ResultType"].ToString();
            if (resultType.Equals("Chart"))
            {
                generateChart("学费");
            }
           




            ddl_major.Items.Clear();
            string value = ddl_academy.SelectedItem.Value.ToString();
            DataTable major = academyManager.getMajorTable();

            if (value.Equals("0"))
            {
                //ddl_major.Items.Clear();
                ddl_major.Items.Insert(0, new System.Web.UI.WebControls.ListItem("请选择专业", "0"));
                for (int i = 0; i < major.Rows.Count; i++)
                {
                    DataRow row = major.Rows[i];
                    ddl_major.Items.Add(new System.Web.UI.WebControls.ListItem(row["majName"].ToString(), row["majId"].ToString()));
                }
            }
            else
            {
                ddl_major.Items.Insert(0, new System.Web.UI.WebControls.ListItem("请选择专业", "0"));
                for (int i = 0; i < major.Rows.Count; i++)
                {
                    DataRow row = major.Rows[i];
                    string acaId = row["acaId"].ToString();
                    if (acaId.Equals(value))
                    {
                        ddl_major.Items.Add(new System.Web.UI.WebControls.ListItem(row["majName"].ToString(), row["majId"].ToString()));
                    }
                }
                if (ddl_major.Items.Count == 0)
                {
                    ddl_major.Items.Insert(0, new System.Web.UI.WebControls.ListItem("请选择年级", "0"));
                }
            }
            loadClassDDL();
        }

        protected void ddl_major_SelectedIndexChanged(object sender, EventArgs e)
        {
            String resultType = ViewState["ResultType"].ToString();
            if (resultType.Equals("Chart"))
            {
                generateChart("学费");
            }
            loadClassDDL();
        }

        protected void ddl_grade_SelectedIndexChanged(object sender, EventArgs e)
        {
            String resultType = ViewState["ResultType"].ToString();
            if (resultType.Equals("Chart"))
            {
                generateChart("学费");
            }
            loadClassDDL();
        }

        protected void ddl_class_SelectedIndexChanged(object sender, EventArgs e)
        {
            String resultType = ViewState["ResultType"].ToString();
            if (resultType.Equals("Chart"))
            {
                generateChart("学费");
            }
        }

        protected void ddl_year_SelectedIndexChanged(object sender, EventArgs e)
        {
            String resultType = ViewState["ResultType"].ToString();
            if (resultType.Equals("Chart"))
            {
                generateChart("学费");
            }
        }


        /// <summary>
        /// 点击查找按钮
        /// </summary>
        protected void btn_Search_Click(object sender, EventArgs e)
        {
            GV_SelectedResult.PageIndex = 0;
            string text = ddl_type.SelectedItem.Text.ToString();
            bindGridView(text);
        }


        private void bindGridView(string selectedType)
        {
            switch (selectedType)
            {
                case "请选择":
                    ViewState["SearchResultSwitch"] = "";
                    break;
                case "学费":
                    ViewState["SearchResultSwitch"] = "学费";
                    searchTuitionAction("name");
                    break;
                case "双专业费":
                    ViewState["SearchResultSwitch"] = "双专业费";
                    break;
                case "水电费":
                    ViewState["SearchResultSwitch"] = "水电费";
                    searchUtilityAction();
                    break;
                case "校园网费":
                    ViewState["SearchResultSwitch"] = "校园网费";
                    //searchNetworkBillAction();
                    break;
                case "学生卡充值":
                    ViewState["SearchResultSwitch"] = "学生卡充值";
                    //searchStudentCardAction();
                    break;
                default:
                    break;
            }
        }

        


        private void searchTuitionAction(string sortOrder)
        {
            string acaValue = ddl_academy.SelectedItem.Value.ToString();
            string majValue = ddl_major.SelectedItem.Value.ToString();
            string graValue = ddl_grade.SelectedItem.Value.ToString();
            string classValue = ddl_class.SelectedItem.Value.ToString();
            string yearValue = ddl_year.SelectedItem.Value.ToString();
            DataSet ds = searchManager.searchTuition(acaValue, majValue, graValue, classValue, yearValue);
            //ViewState["DataSet"] = ds;
            int dsRowsCount = ds.Tables[0].Rows.Count;
            int tuition_unPaidNum = 0;
            int tuition_paidNum = 0;
            decimal tuition_totalNeedPayAmount = Convert.ToDecimal("0");
            decimal tuition_totalHasPaidAmount = Convert.ToDecimal("0");

            for (int i = 0; i < dsRowsCount; i++)
            {
                String projectStatus = ds.Tables[0].Rows[i]["projectStatus"].ToString();
                tuition_totalNeedPayAmount += Convert.ToDecimal(ds.Tables[0].Rows[i]["totalAmount"]);
                tuition_totalHasPaidAmount += Convert.ToDecimal(ds.Tables[0].Rows[i]["totalPaidAmount"]);
                if (projectStatus.Equals("未缴清"))
                {
                    tuition_unPaidNum++;
                }
                else if (projectStatus.Equals("已缴完"))
                {
                    tuition_paidNum++;
                }
            }
            //<%=GV_SelectedResult.Rows.Count%>
            lbl_tuition_rowCount.Text = GV_SelectedResult.Rows.Count.ToString();
            lbl_tuition_unPaidNum.Text = tuition_unPaidNum.ToString();
            lbl_tuition_paidNum.Text = tuition_paidNum.ToString();
            lbl_tuition_totalNeedPayAmount.Text = tuition_totalNeedPayAmount.ToString();
            lbl_tuition_totalHasPaidAmount.Text = tuition_totalHasPaidAmount.ToString();
            DataView view = new DataView(ds.Tables[0]);
            view.Sort = sortOrder + " " + ViewState["OrderDir"].ToString();


            int columnNum = 6;
            String[] headerStr = new String[columnNum];
            headerStr[0] = "学号";
            headerStr[1] = "姓名";
            headerStr[2] = "缴费学费项目";
            headerStr[3] = "缴费状态";
            headerStr[4] = "应缴金额";
            headerStr[5] = "已缴金额";

            String[] dataFieldName = new String[columnNum];
            dataFieldName[0] = "stuId";
            dataFieldName[1] = "name";
            dataFieldName[2] = "projectName";
            dataFieldName[3] = "projectStatus";
            dataFieldName[4] = "totalAmount";
            dataFieldName[5] = "totalPaidAmount";

            Unit[] widths = new Unit[columnNum];
            widths[0] = new Unit("13%");
            widths[1] = new Unit("12%");
            widths[2] = new Unit("27%");
            widths[3] = new Unit("19%");
            widths[4] = new Unit("19%");
            widths[5] = new Unit("19%");

            GV_SelectedResult.Columns.Clear();

            for (int i = 0; i < headerStr.Length; i++)
            {
                BoundField field = new BoundField();
                field.HeaderText = headerStr[i];
                field.DataField = dataFieldName[i];
                field.SortExpression = dataFieldName[i];
                field.ItemStyle.Width = widths[i];
                GV_SelectedResult.Columns.Add(field);
            }


            //HiddenField
            /*BoundField hiddenField1 = new BoundField();
            hiddenField1.HeaderText = "学院";
            hiddenField1.DataField = "academy";
            hiddenField1.SortExpression = "academy";
            hiddenField1.ItemStyle.Width = new Unit("0%");
            hiddenField1.Visible = false;
            GV_SelectedResult.Columns.Add(hiddenField1);

            BoundField hiddenField2 = new BoundField();
            hiddenField2.HeaderText = "专业";
            hiddenField2.DataField = "firstMajor";
            hiddenField2.SortExpression = "firstMajor";
            hiddenField2.ItemStyle.Width = new Unit("0%");
            hiddenField2.Visible = false;
            GV_SelectedResult.Columns.Add(hiddenField2);

            BoundField hiddenField3 = new BoundField();
            hiddenField3.HeaderText = "班级";
            hiddenField3.DataField = "classNum";
            hiddenField3.SortExpression = "classNum";
            hiddenField3.ItemStyle.Width = new Unit("0%");
            hiddenField3.Visible = false;
            GV_SelectedResult.Columns.Add(hiddenField3);

            BoundField hiddenField4 = new BoundField();
            hiddenField4.HeaderText = "年级";
            hiddenField4.DataField = "grade";
            hiddenField4.SortExpression = "grade";
            hiddenField4.ItemStyle.Width = new Unit("0%");
            hiddenField4.Visible = false;
            GV_SelectedResult.Columns.Add(hiddenField4);

            BoundField hiddenField5 = new BoundField();
            hiddenField5.HeaderText = "年份";
            hiddenField5.DataField = "year";
            hiddenField5.SortExpression = "year";
            hiddenField5.ItemStyle.Width = new Unit("0%");
            hiddenField5.Visible = false;
            GV_SelectedResult.Columns.Add(hiddenField5);*/





            initGridView(headerStr, dataFieldName, widths);

            GV_SelectedResult.DataSource = view;
            GV_SelectedResult.DataBind();
        }


        /*水费查询*/
        private void searchUtilityAction()
        {
            string uitilityYearValue = ddl_uitilityYear.SelectedItem.Value.ToString();
            string uitilityMonthValue = ddl_uitilityMonth.SelectedItem.Value.ToString();
            string apartmentValue = ddl_apartment.SelectedItem.Value.ToString();
            string dorNumValue = txt_dorNum.Text.ToString();

            DataSet ds = searchManager.searchUtility(uitilityYearValue, uitilityMonthValue, apartmentValue, dorNumValue);
            DataRowCollection drc = ds.Tables[0].Rows;
            int dsRowsCount = drc.Count;
            //DataColumnCollection dcc = ds.Tables[0].Columns;
            /*DataColumn waterBill = new DataColumn();
            waterBill.ColumnName = "水费";
            DataColumn electricBill = new DataColumn();
            electricBill.ColumnName = "电费";
            ds.Tables[0].Columns.add(waterBill);*/
            //ds.Tables[0].Columns.Add("waterBill", Type.GetType("System.String"));
            //ds.Tables[0].Columns.Add("electricBill", Type.GetType("System.String"));

            /*DataRow curRow;
            for (int i = 0; i < dsRowsCount; i++)
            {
                //drc[i]["waterUseNum"] = drc[i]["waterUseNum"]
                curRow = drc[i];
                
                UtilityPaymentProject utilityPaymentProject =  utilityPaymentManager.GetUtilityPaymentInforByDorId(drc[i]["dorId"].ToString());
                //String test1 = utilityPaymentProject.WaterBill.ToString();
                //String test2 = utilityPaymentProject.ElectricityBill.ToString();
                curRow["waterBill"] = utilityPaymentProject.WaterBill.ToString();
                curRow["electricBill"] = utilityPaymentProject.ElectricityBill.ToString();
            }*/
            
            

            /*for (int i = 0; i < dsRowsCount; i++)
            {
                String projectStatus = ds.Tables[0].Rows[i]["projectStatus"].ToString();
                if (projectStatus.Equals("未缴清"))
                {
                    tuition_unPaidNum++;
                }
                else if (projectStatus.Equals("已缴完"))
                {
                    tuition_paidNum++;
                }
            }*/

            DataView view = new DataView(ds.Tables[0]);
            view.Sort = "dorId" + " " + ViewState["OrderDir"].ToString();


            int columnNum = 8;
            String[] headerStr = new String[columnNum];
            headerStr[0] = "缴费年月";
            headerStr[1] = "公寓楼";
            headerStr[2] = "宿舍号";
            headerStr[3] = "水费使用情况";
            headerStr[4] = "应缴水费";
            headerStr[5] = "电费使用情况";
            headerStr[6] = "应缴电费";
            headerStr[7] = "缴费状态";

            String[] dataFieldName = new String[columnNum];
            dataFieldName[0] = "date";
            dataFieldName[1] = "apaName";
            dataFieldName[2] = "dorNum";
            dataFieldName[3] = "waterUseNum";
            dataFieldName[4] = "waterBill";
            dataFieldName[5] = "elecUseNum";
            dataFieldName[6] = "elecBill";
            dataFieldName[7] = "status";

            Unit[] widths = new Unit[columnNum];
            widths[0] = new Unit("10%");
            widths[1] = new Unit("20%");
            widths[2] = new Unit("10%");
            widths[3] = new Unit("10%");
            widths[4] = new Unit("15%");
            widths[5] = new Unit("10%");
            widths[6] = new Unit("15%");
            widths[7] = new Unit("10%");

            initGridView(headerStr, dataFieldName, widths);

            GV_SelectedResult.DataSource = view;
            GV_SelectedResult.DataBind();
        }




        

        /*校园网费查询*/
        /*private void searchNetworkBillAction()
        {
            string nbYearValue = ddl_NB_Year.SelectedItem.Value.ToString();
            string nbMonthValue = ddl_NB_Month.SelectedItem.Value.ToString();
            string nbApartmentValue = ddl_NB_Apartment.SelectedItem.Value.ToString();
            string nbDorNumValue = txt_NB_DorNum.Text.ToString();
            string nbPortNumValue = ddl_NB_PortNum.Text.ToString();

            DataSet ds = searchManager.searchUtility(nbYearValue, nbMonthValue, nbApartmentValue, nbDorNumValue, nbPortNumValue);
            DataRowCollection drc = ds.Tables[0].Rows;
            int dsRowsCount = drc.Count;
          

            DataView view = new DataView(ds.Tables[0]);
            view.Sort = "dorId" + " " + ViewState["OrderDir"].ToString();


            int columnNum = 8;
            String[] headerStr = new String[columnNum];
            headerStr[0] = "缴费年月";
            headerStr[1] = "公寓楼";
            headerStr[2] = "宿舍号";
            headerStr[3] = "水费使用情况";
            headerStr[4] = "应缴水费";
            headerStr[5] = "电费使用情况";
            headerStr[6] = "应缴电费";
            headerStr[7] = "缴费状态";

            String[] dataFieldName = new String[columnNum];
            dataFieldName[0] = "date";
            dataFieldName[1] = "apaName";
            dataFieldName[2] = "dorNum";
            dataFieldName[3] = "waterUseNum";
            dataFieldName[4] = "waterBill";
            dataFieldName[5] = "elecUseNum";
            dataFieldName[6] = "elecBill";
            dataFieldName[7] = "status";

            Unit[] widths = new Unit[columnNum];
            widths[0] = new Unit("10%");
            widths[1] = new Unit("20%");
            widths[2] = new Unit("10%");
            widths[3] = new Unit("10%");
            widths[4] = new Unit("15%");
            widths[5] = new Unit("10%");
            widths[6] = new Unit("15%");
            widths[7] = new Unit("10%");

            initGridView(headerStr, dataFieldName, widths);

            GV_SelectedResult.DataSource = view;
            GV_SelectedResult.DataBind();
        }*/


        /*学生卡充值情况查询*/
        /*private void searchStudentCardAction()
        {
            string nbYearValue = ddl_NB_Year.SelectedItem.Value.ToString();
            string nbMonthValue = ddl_NB_Month.SelectedItem.Value.ToString();
            string nbApartmentValue = ddl_NB_Apartment.SelectedItem.Value.ToString();
            string nbDorNumValue = txt_NB_DorNum.Text.ToString();
            string nbPortNumValue = ddl_NB_PortNum.Text.ToString();

            DataSet ds = searchManager.searchUtility(nbYearValue, nbMonthValue, nbApartmentValue, nbDorNumValue, nbPortNumValue);
            DataRowCollection drc = ds.Tables[0].Rows;
            int dsRowsCount = drc.Count;
          

            DataView view = new DataView(ds.Tables[0]);
            view.Sort = "dorId" + " " + ViewState["OrderDir"].ToString();


            int columnNum = 8;
            String[] headerStr = new String[columnNum];
            headerStr[0] = "缴费年月";
            headerStr[1] = "公寓楼";
            headerStr[2] = "宿舍号";
            headerStr[3] = "水费使用情况";
            headerStr[4] = "应缴水费";
            headerStr[5] = "电费使用情况";
            headerStr[6] = "应缴电费";
            headerStr[7] = "缴费状态";

            String[] dataFieldName = new String[columnNum];
            dataFieldName[0] = "date";
            dataFieldName[1] = "apaName";
            dataFieldName[2] = "dorNum";
            dataFieldName[3] = "waterUseNum";
            dataFieldName[4] = "waterBill";
            dataFieldName[5] = "elecUseNum";
            dataFieldName[6] = "elecBill";
            dataFieldName[7] = "status";

            Unit[] widths = new Unit[columnNum];
            widths[0] = new Unit("10%");
            widths[1] = new Unit("20%");
            widths[2] = new Unit("10%");
            widths[3] = new Unit("10%");
            widths[4] = new Unit("15%");
            widths[5] = new Unit("10%");
            widths[6] = new Unit("15%");
            widths[7] = new Unit("10%");

            initGridView(headerStr, dataFieldName, widths);

            GV_SelectedResult.DataSource = view;
            GV_SelectedResult.DataBind();
        }*/








        protected void GV_SearchedTuition_Sorting(object sender, GridViewSortEventArgs e)
        {
            string sortExpression = e.SortExpression;

            if (ViewState["OrderDir"].ToString() == "DESC")
            {
                ViewState["OrderDir"] = "ASC";
            }
            else
            if (ViewState["OrderDir"].ToString() == "ASC")
            {
                ViewState["OrderDir"] = "DESC";
            }
            searchTuitionAction(sortExpression);
        }





        /// <summary>
        /// 初始化GridView
        /// </summary>
        /// <param name="HearStr"></param>
        /// <param name="DataFieldName"></param>
        /// <param name="widths"></param>
        private void initGridView(String []HearStr,String []DataFieldName,Unit[] widths)
        {
            GV_SelectedResult.Columns.Clear();

            for (int i = 0; i < HearStr.Length; i++)
            {

                BoundField field = new BoundField();
                field.HeaderText = HearStr[i];
                field.DataField = DataFieldName[i];
                field.SortExpression = DataFieldName[i];
                field.ItemStyle.Width = widths[i];
                GV_SelectedResult.Columns.Add(field);
            }

        }

        protected void btn_ShowDetail_Click(object sender, EventArgs e)
        {
            ViewState["SearchResultSwitch"] = "学费详细";
            string acaValue = ddl_academy.SelectedItem.Value.ToString();
            string majValue = ddl_major.SelectedItem.Value.ToString();
            string graValue = ddl_grade.SelectedItem.Value.ToString();
            string classValue = ddl_class.SelectedItem.Value.ToString();
            string yearValue = ddl_year.SelectedItem.Value.ToString();
            DataSet ds = searchManager.searchTuitionDetail(acaValue, majValue, graValue, classValue, yearValue);
            int dsRowsCount = ds.Tables[0].Rows.Count;

           
            DataView view = new DataView(ds.Tables[0]);
            view.Sort = "stuId" + " " + ViewState["OrderDir"].ToString();



            int columnNum = 7;
            String[] headerStr = new String[columnNum];
            headerStr[0] = "学号";
            headerStr[1] = "姓名";
            headerStr[2] = "缴费学费项目";
            headerStr[3] = "包含详细项名称";
            headerStr[4] = "详细项缴费状态";
            headerStr[5] = "详细项应缴金额";
            headerStr[6] = "详细项已缴金额";
            

            String[] dataFieldName = new String[columnNum];
            dataFieldName[0] = "stuId";
            dataFieldName[1] = "name";
            dataFieldName[2] = "projectName";
            dataFieldName[3] = "detailName";
            dataFieldName[4] = "detailStatus";
            dataFieldName[5] = "amountFcPay";
            dataFieldName[6] = "amountShPay";

            Unit[] widths = new Unit[columnNum];
            widths[0] = new Unit("12%");
            widths[1] = new Unit("8%");
            widths[2] = new Unit("25%");
            widths[3] = new Unit("15%");
            widths[4] = new Unit("10%");
            widths[5] = new Unit("15%");
            widths[6] = new Unit("15%");


            initGridView(headerStr, dataFieldName, widths);

            GV_SelectedResult.DataSource = view;
            GV_SelectedResult.DataBind();




        }



        /// <summary>
        /// 生成报表
        /// </summary>
        protected void imgbtn_generateReport_Click(object sender, EventArgs e)
        {
            ViewState["ResultType"] = "Report";

            /*int gv_columnNum = GV_SelectedResult.Columns.Count;
            int[] widths = new int[gv_columnNum];  //根据列数目而定
            for (int i = 0; i < gv_columnNum; i++)
            {
                widths[i] = Convert.ToInt32(GV_SelectedResult.Columns[i].ItemStyle.Width.Value.ToString());
            }

            String statisticsInfo = "\n\n 总计 " + GV_SelectedResult.Rows.Count + " 条数据，" +
                                "其中，已缴费学生有 " + lbl_tuition_paidNum.Text + " 名，未缴清学生有 " + lbl_tuition_unPaidNum.Text + " 名。\n" +
                                "总计须缴总金额：" + lbl_tuition_totalNeedPayAmount.Text + " 元，已缴总金额 " + lbl_tuition_totalHasPaidAmount.Text + " 元。";

            PrintReportHelper.printStatisticsReport("学费统计报表", GV_SelectedResult, Server.MapPath("../Output/test.pdf"), widths, statisticsInfo);
            */


        }


        /// <summary>
        /// 生成图表
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void imgbtn_generateChart_Click(object sender, EventArgs e)
        {
            ViewState["ResultType"] = "Chart";

            string text = ddl_type.SelectedItem.Text.ToString();
            generateChart(text);

            /*String paymentTypeChart = ViewState["PaymentTypeChart"].ToString();
            switch (paymentTypeChart)
            {
                case "":
                    break;
                case "Tuition_AllSelect":
                    
                    break;
            }*/

          

        }



        private void generateChart(string selectedType)
        {
            switch (selectedType)
            {
                case "请选择":
                    ViewState["SearchResultSwitch"] = "";
                    break;
                case "学费":
                    ViewState["SearchResultSwitch"] = "学费";
                    drawTuitionChart();
                    break;
                case "双专业费":
                    ViewState["SearchResultSwitch"] = "双专业费";
                    break;
                case "水电费":
                    ViewState["SearchResultSwitch"] = "水电费";
                    break;
                case "校园网费":
                    ViewState["SearchResultSwitch"] = "校园网费";
                    break;
                case "学生卡充值":
                    ViewState["SearchResultSwitch"] = "学生卡充值";
                    break;
                default:
                    break;
            }
        }




        private void drawTuitionChart()
        {

            string acaValue = ddl_academy.SelectedItem.Value.ToString();
            string majValue = ddl_major.SelectedItem.Value.ToString();
            string graValue = ddl_grade.SelectedItem.Value.ToString();
            string classValue = ddl_class.SelectedItem.Value.ToString();
            string yearValue = ddl_year.SelectedItem.Value.ToString();
            bool chosedAcademy = !acaValue.Equals("0");
            bool chosedMajor = !majValue.Equals("0");
            bool chosedGrade = !graValue.Equals("0");
            bool chosedClass = !classValue.Equals("0");
            bool chosedYear = !yearValue.Equals("0");
            //DataSet ds = searchManager.searchTuition(acaValue, majValue, graValue, classValue, yearValue);
            //ArrayList ColorGuider = new ArrayList();
            //ArrayList[] DataArray = { new ArrayList(), new ArrayList(), new ArrayList() };
            //ArrayList XLabel = new ArrayList();
            //int dsRowsCount = ds.Tables[0].Rows.Count;
            DataSet ds = null;

            
            if (!chosedAcademy && !chosedMajor && !chosedGrade && !chosedClass && !chosedYear)
            {
                Chartlet1.ChartTitle.Text = "各学院缴费情况统计图";
                ds = chartManager.getAcademyStatisticInfo();
                Chartlet1.BindChartData(ds);
                Chartlet1.YLabels.UnitText = "人数";
                //ViewState["ChartData"] = ds;
            }
            else if (chosedAcademy && !chosedMajor && !chosedGrade && !chosedClass && !chosedYear)
            {
                string acaText = ddl_academy.SelectedItem.Text.ToString();
                Chartlet1.ChartTitle.Text = acaText + "缴费情况统计图";
                ds = chartManager.getAcademyStatisticInfo(acaValue);
                Chartlet1.BindChartData(ds);
                Chartlet1.YLabels.UnitText = "人数";
            }
            else if (chosedAcademy && chosedMajor && !chosedGrade && !chosedClass && !chosedYear)
            {
                string acaText = ddl_academy.SelectedItem.Text.ToString();
                string majText = ddl_major.SelectedItem.Text.ToString();
                Chartlet1.ChartTitle.Text = acaText + majText + "缴费情况统计图";
                ds = chartManager.getMajorStatisticInfo(acaValue,majValue);
                Chartlet1.BindChartData(ds);
                Chartlet1.YLabels.UnitText = "人数";
            }
            else if (chosedAcademy && chosedMajor && chosedGrade && !chosedClass && !chosedYear)
            {
                string acaText = ddl_academy.SelectedItem.Text.ToString();
                string majText = ddl_major.SelectedItem.Text.ToString();
                string gradeText = ddl_grade.SelectedItem.Text.ToString();
                Chartlet1.ChartTitle.Text = acaText + majText + gradeText + "级缴费情况统计图";
                ds = chartManager.getGradeStatisticInfo(acaValue, majValue, graValue);
                Chartlet1.BindChartData(ds);
                Chartlet1.YLabels.UnitText = "人数";
            }
            else if (chosedAcademy && chosedMajor && chosedGrade && chosedClass && !chosedYear)
            {
                string acaText = ddl_academy.SelectedItem.Text.ToString();
                string majText = ddl_major.SelectedItem.Text.ToString();
                string gradeText = ddl_grade.SelectedItem.Text.ToString();
                string classText = ddl_class.SelectedItem.Text.ToString();
                Chartlet1.ChartTitle.Text = acaText + majText+"专业" + gradeText + classText+ "缴费情况统计图";
                ds = chartManager.getClassStatisticInfo(classValue);

                Chartlet1.BindChartData(ds);
                Chartlet1.YLabels.UnitText = "人数";
            }



        }








        protected void GV_SelectedResult_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            

            // 得到该控件
            GridView theGrid = sender as GridView;
            int newPageIndex = 0;
            if (e.NewPageIndex == -3)
            {
                //点击了Go按钮
                TextBox txtNewPageIndex = null;

                //GridView较DataGrid提供了更多的API，获取分页块可以使用BottomPagerRow 或者TopPagerRow，当然还增加了HeaderRow和FooterRow
                GridViewRow pagerRow = theGrid.BottomPagerRow;

                if (pagerRow != null)
                {
                    //得到text控件
                    txtNewPageIndex = pagerRow.FindControl("txtNewPageIndex") as TextBox;
                }
                if (txtNewPageIndex != null)
                {
                    //得到索引
                    newPageIndex = int.Parse(txtNewPageIndex.Text) - 1;
                }
            }
            else
            {
                //点击了其他的按钮
                newPageIndex = e.NewPageIndex;
            }

            //防止新索引溢出
            newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
            newPageIndex = newPageIndex >= theGrid.PageCount ? theGrid.PageCount - 1 : newPageIndex;

            //得到新的值
            theGrid.PageIndex = newPageIndex;

            //重新绑定
            string text = ddl_type.SelectedItem.Text.ToString();
            bindGridView(text);

        }

        
        





    }



    public class GridViewTemplate : ITemplate
    {
        public delegate void EventHandler(object sender, EventArgs e);
        public event EventHandler eh;

        private DataControlRowType templateType;

        private string columnName;
        private string controlID;



        public GridViewTemplate(DataControlRowType type, string colname)
        {

            templateType = type;

            columnName = colname;

        }

        public GridViewTemplate(DataControlRowType type, string controlID, string colname)
        {
            templateType = type;
            this.controlID = controlID;
            columnName = colname;
        }


        public void InstantiateIn(System.Web.UI.Control container)
        {
            switch (templateType)
            {
                case DataControlRowType.Header:
                    Literal lc = new Literal();
                    lc.Text = columnName;
                    container.Controls.Add(lc);
                    break;
                case DataControlRowType.DataRow:
                    LinkButton lbtn = new LinkButton();
                    lbtn.ID = this.controlID;
                    if (eh != null)
                    {
                        lbtn.Click += new System.EventHandler(eh);
                    }
                    lbtn.DataBinding += new System.EventHandler(lbtn_DataBinding);

                    container.Controls.Add(lbtn);

                    break;
                default:
                    break;
            }
        }
        void lbtn_DataBinding(object sender, EventArgs e)
        {
            LinkButton lbtn = sender as LinkButton;
            if (lbtn != null)
            {
                GridViewRow container = lbtn.NamingContainer as GridViewRow;
                if (container != null)
                {
                    object dataValue = DataBinder.Eval(container.DataItem, columnName);
                    if (dataValue != DBNull.Value)
                    {
                        lbtn.Text = dataValue.ToString();
                    }
                }
            }
        }
    }

}